﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HashFoo.Rest.Common;
using HashFoo.Rest.Server;
using HashFoo.Rest.ServerTests.FakeHttpStack;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HashFoo.Rest.ServerTests
{
    [TestClass]
    public class ResourceSerializerFilterTests
    {
        [TestMethod]
        public void fallback_on_default_if_accepttype_is_empty()
        {
            var filter = new ResourceSerializerFilter(Enumerable.Empty<IResourceSerializer>());
            var result = new ResourceActionResult("result");
            var httpContext = new FakeHttpContext();
            var context = new ResultExecutingContext { Result = result, HttpContext = httpContext };

            filter.OnResultExecuting(context);

            Assert.IsTrue(result.Content == "result");
        }

        [TestMethod]
        public void fallback_on_default_if_no_accepttype_match()
        {
            var filter = new ResourceSerializerFilter(new[] { new MockResources.JsonSerializerMock() });
            var result = new ResourceActionResult("result");
            var httpContext = new FakeHttpContext();
            httpContext.FakeRequest.SetAcceptTypes(new[] { "nothing" });
            var context = new ResultExecutingContext { Result = result, HttpContext = httpContext };

            filter.OnResultExecuting(context);

            Assert.IsTrue(result.Content == "result");
        }

        [TestMethod]
        public void uses_serializer_if_accepttype_matched()
        {
            var filter = new ResourceSerializerFilter(new[] { new MockResources.JsonSerializerMock() });
            var result = new ResourceActionResult("result");
            const string contentType = "application/json";
            var httpContext = new FakeHttpContext();
            httpContext.FakeRequest.SetAcceptTypes(new[] { contentType });
            var context = new ResultExecutingContext { Result = result, HttpContext = httpContext };

            filter.OnResultExecuting(context);

            Assert.IsTrue(result.Content == "result");
            Assert.AreEqual(contentType, result.ContentType);
        }
    }
}
